import router from './router'
import { store, key } from './store'
import { ElMessage } from 'element-plus'
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'
import { getToken } from './utils/auth'
import getPageTitle from './utils/get-page-title'

NProgress.configure({ showSpinner: false })

// 白名单
const whiteList = ['/login']

router.beforeEach((to, from, next) => {
  NProgress.start()

  //   设置网页标题
  //   document.title = getPageTitle(to.meta.title)
  if (getToken()) {
    //token存在
    if (to.path === '/login') {
      next({ path: '/login' })
      NProgress.done()
    } else {
      // 1.判断是否获取到角色
      // 2.根据角色拉取userInfo信息
      // 3.根据userinfo拉取动态路由表,通过router.addRoutes()
      // 4.next({...to,replace:true}) hack方法 确保addRoutes已完成
    }
  } else {
    //没有token,在白名单直接去,否则去login页面
    if (whiteList.includes(to.path)) {
      next()
    } else {
      next({ path: '/login' })
      NProgress.done()
    }
  }
})

router.afterEach(() => {
  NProgress.done()
})
